﻿Imports System.Windows.Forms
Imports TBCC.Utility

Public Class UserEditForm

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.Text = "新增用户"
#If LSC_VERSION_SWITCH = "PROJECT" Then
        Me.optHighLevel.Enabled = True
#ElseIf LSC_VERSION_SWITCH = "USER" Then
        Me.optHighLevel.Enabled = False
#End If
    End Sub

    Public Sub New(ByVal ui As UserItem)
        MyClass.New()

        _ui = ui

        Me.Text = "修改用户"
        UserIDTextBox.ReadOnly = True

        LoadUserInfo()
    End Sub

    Private _ui As UserItem = Nothing

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If checkValidate() = False Then
            Exit Sub
        End If

        If Save() = False Then
            Exit Sub
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Function CheckValidate() As Boolean
        If Me.UserIDTextBox.Text.Trim = "" Then
            msg.Show("用户名不能为空")
            UserIDTextBox.Focus()
            Return False
        End If

        Dim bFound As Boolean = False
        For Each ctl As RadioButton In Me.Panel1.Controls
            If ctl.Checked Then
                bFound = True
                Exit For
            End If
        Next
        If Not bFound Then
            msg.Show("请选择角色")
            Me.Panel1.Focus()
            Return False
        End If
        Return True
    End Function

    Private Function GetSelectedRoleID() As Integer
        Dim id As Integer = 0
        For Each ctl As RadioButton In Me.Panel1.Controls
            If ctl.Checked Then
                id = ctl.Tag
                Exit For
            End If
        Next

        Return id
    End Function

    Public Sub LoadUserInfo()
        Me.UserIDTextBox.Text = _ui.uID
        Me.RealNameTextBox.Text = _ui.uName
        Select Case _ui.roleID
            Case 1
                Me.optAdmin.Checked = True

            Case 2
                Me.optHighLevel.Checked = True

            Case 3
                Me.optNormal.Checked = True

            Case 4
                Me.optGuest.Checked = True

        End Select
    End Sub

    Private Function Save() As Boolean
        
        If _ui Is Nothing Then
            Return AddUser()
        Else
            Return UpdateUser()
        End If
        Return False
    End Function

    Private Function AddUser() As Boolean
        Dim uId As String = UserIDTextBox.Text.Trim
        Dim uName As String = RealNameTextBox.Text.Trim
        Dim uPwd As String = PasswordTextBox.Text.Trim

        Dim roleID As Integer
        roleID = GetSelectedRoleID()

        Dim result As Integer
        result = UserClass.Create(uId, uName, uPwd, roleID)

        Return result = 0

    End Function

    Private Function UpdateUser() As Boolean
        Dim uId As String = UserIDTextBox.Text.Trim
        Dim uName As String = RealNameTextBox.Text.Trim
        Dim uPwd As String = PasswordTextBox.Text.Trim


        Dim roleID As Integer
        roleID = GetSelectedRoleID()

        Dim result As Integer
        result = UserClass.Update(uId, uName, uPwd, roleID)

        Return result = 0
    End Function
 
    Private Sub UserEditForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
